Titanium.UI.setBackgroundColor('#000');

var db = Titanium.Database.open ('dagboksDB');

db.execute('CREATE TABLE IF NOT EXISTS child (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT)');
db.execute('CREATE TABLE IF NOT EXISTS dagbok (ID INTEGER PRIMARY KEY AUTOINCREMENT, CHILDID INTEGER, RUBRIK TEXT, POST TEXT, POSTDATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP )');

var rows = db.execute('SELECT * FROM dagbok');
  if (!(rows.isValidRow())) {
    db.execute('INSERT INTO dagbok (CHILDID, RUBRIK, POST) VALUES(?,?,?)', '1', 'Hello and Welcome', 'Post your life here');
  }
rows.close();

var rows = db.execute('SELECT * FROM child');
  if (!rows.isValidRow()) {
    db.execute('INSERT INTO child (NAME) VALUES(?)', 'All'); 
  }
rows.close();

/*var rows = db.execute('select * from dagbok');
Ti.UI.createAlertDialog({ title: 'debug', message: JSON.stringify(rows)}).show();*/
/*
var win = Ti.UI.createWindow({
 backgroundColor: '#cccccc',
 exitOnClose:true,
 navBarHidden:false
});
*/

var win = Titanium.UI.createWindow({
    title: 'Pocket Diary - family edition',
    exitOnClose: true
});

//win.backgroundImage = 'bilder/splash.jpg';

var tableview = Titanium.UI.createTableView({
  data:[ 
    { leftImage:'bilder/napp.png', title:"Children", color:"white", test:'viewchild.js', textAlign:'center', rightImage:'bilder/pil3.png'},
    { leftImage:'bilder/book2.png', title:"View", color:"white", test:'viewall.js', textAlign:'center', rightImage:'bilder/pil3.png'},
    { leftImage:'bilder/penna.png', title:"Post", color:"white", textAlign:'center', test:'post.js', rightImage:'bilder/pil3.png'},
    { leftImage:'bilder/info.png', title:"Info", color:"white", textAlign:'center', test:'info.js', rightImage:'bilder/pil3.png' },
    { leftImage:'bilder/kamera.png', title:"Quick Pic", color:"white", textAlign:'center', test:'photos.js', rightImage:'bilder/pil3.png' }
    
  ],
  minRowHeight:60,
  width:220,
  top:30,
  height:305,
  borderWidth:1,
  borderRadius:5,
  borderColor:"#ccc",
  backgroundColor:"black"
  });


win.add(tableview);
win.open();

tableview.addEventListener('click', function(e)
{
  if (e.rowData.test)
  {
    var win2 = Titanium.UI.createWindow({
      url:e.rowData.test,
      title:e.rowData.title
    });
    //win.close();
    win2.open({modal:true});
  }
});


//including av menyknapp
var menu = Ti.UI.Android.OptionMenu.createMenu();
  Ti.include("menu.js");
